﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestGenerator
{
    class TestGenerator
    {
        #region TagConstants
        const string bodyOpen=@"<html><head><meta http-equiv=""Content-Type"" content=""text/html; charset=windows-1251"">
<title>Test answers</title>
<style></style>
</head>
<body lang=BG>
<div class=Section1>";
        const string bodyClose="<p class=Question>&nbsp;</p></div></body></html>";
        const string tableOpen=@"<table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0 style='margin-left:.25pt;border-collapse:collapse;border:none'>";
        const string tableClose="</table>";
        const string rowOpen="<tr style='height:3.15pt'>";
        const string rowClose="</tr>";
        const string cellOpen=@"<td width=34 valign=top style='width:25.35pt;border:solid windowtext 1.0pt; padding:2.85pt 0cm 2.85pt 0cm;height:3.15pt'>
  <p class=MsoNormal align=center style='text-align:center;text-indent:0cm'><span
  style='font-size:10.0pt;font-family:Tahoma'>";
        const string cellClose="</span></p></td>";

        #endregion TagConstants

        static void Main(string[] args)
        {
            TestGenerator testGenerator=new TestGenerator(settings);
            testGenerator.GenerateTestsAndAnswers();
        }

        public void GenerateTestsAndAnswers()
        {      
            Settings settings=new Settings(@"Data/TestGenerator.ini");
            Test test=new Test(settings);
            List<char> correctList=new List<char>();

            for (int i=1; i <= settings.NumberOfTests; i++)
            {
                test.GenerateNext();
                string generatedTest=test.ToString(settings, i, correctList);

                string outTestFilename=string.Format("test{0:D3}.html", i);

                using (StreamWriter writer=new StreamWriter(outTestFilename, false, Encoding.GetEncoding(1251)))
                {
                    writer.WriteLine(generatedTest);
                }
            }
            WriteAnswers(settings, correctList);
        }

        private void WriteAnswers(Settings settings, List<char> answerList)
        {
            int numberOfTests=settings.NumberOfTests;

            using (StreamWriter answerWriter=new StreamWriter(settings.AnswersOutput, false, Encoding.GetEncoding(1251)))
            {
                answerWriter.Write(bodyOpen);
                answerWriter.Write(tableOpen);
                answerWriter.Write(rowOpen);
                answerWriter.Write(cellOpen);
                answerWriter.Write("Вар.");
                answerWriter.Write(cellClose);

                int questions=answerList.Count / numberOfTests;
                for (int i=1; i <= questions; i++)
                {
                    answerWriter.Write(cellOpen);
                    answerWriter.Write(i);
                    answerWriter.Write(cellClose);
                }
                answerWriter.Write(rowClose);
                for (int i=1; i <= numberOfTests; i++)
                {
                    answerWriter.Write(rowOpen);
                    answerWriter.Write(cellOpen);
                    answerWriter.Write(i.ToString("D3"));
                    answerWriter.Write(cellClose);
                    for (int j=1; j <= questions; j++)
                    {
                        answerWriter.Write(cellOpen);
                        answerWriter.Write(answerList[(i - 1) * questions + (j - 1)]);
                        answerWriter.Write(cellClose);
                    }
                    answerWriter.Write(rowClose);
                }
                answerWriter.Write(tableClose);
                answerWriter.Write(bodyClose);
            }
        }
    }
}
